<!doctype html>
<html class="no-js" lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <title>Andy.tool</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link rel="stylesheet" href="/resources/common/css/bootstrap4/bootstrap.min.css" >
        <link rel="stylesheet" href="/resources/common/css/bootstrap4/bootstrap-dialog.min.css" >
        <link rel="stylesheet" href="/resources/common/css/font-awesome.min.css" >
        <link rel="stylesheet" href="/resources/common/Shards-Version-2.0.3/css/shards.min.css?version=2.0.1">
        <link rel="stylesheet" href="/resources/common/Shards-Version-2.0.3/css/shards-extras.min.css?version=2.0.1">
    </head>
<body >
     	
  <div class="fixedNavbar">
		<#include "../../navbar.html">
	</div>
	
	<div class="container">
		<div class="form-group">
			<form role="form" action="/file/upload" method="POST" enctype="multipart/form-data" id="andyForm">
	            <div class="form-group">
			        <input type="file" id="file" name="file" class="form-control btn btn-info" />
		        </div>
		        <div class="form-group">
		        	<input type="text" class="form-control" name="verifyCode" id="verifyCode" placeholder="输入验证码===>选择文件===>生成url  { 文件大小需控制在(0,100]M之间] }">
		        	<img src = "/verify/sendcode" style = "float:left;display:inline-block;cursor:pointer;" id = "verify-code-img"/>
		        </div>
		        <br />
		        <div class="form-group">
                  <input type="text" class="form-control" id="fileurl" placeholder="文件url" disabled="">
                </div>
			</form>
		</div>
			
	</div>
	</div>

	
	<!-- <textarea id="text" ></textarea> -->

	<div class="fixedNavbar">
		<#include "../../footer.html">
	</div>


	<script src="/resources/common/js/jquery-3.3.1.min.js" ></script>
	<script src="/resources/common/js/jquery.validate.js" ></script>
	<script src="/resources/common/js/bootstrap4/bootstrap.min.js"></script>
	<script src="/resources/common/js/bootstrap4/bootstrap-dialog.min.js"></script>

  	<script type="text/javascript">
  		$(function(){

  			$("#verify-code-img").bind("click", function(e) {
				this.src = "/verify/sendcode?d=" + new Date(); 
			});

  			$('#file').on('change', function() {
  				if($('#verifyCode').val() == 0 ){
  					BootstrapDialog.show({
           	            title: '提示',
           	            message: '<font color="purple" size="5px;">请输入验证码</font>',
           	            buttons: [{
           	                label: '知道了',
           	                action: function(dialog) {
           	                    dialog.close();
           	                }
           	            }]
	           	    })
  					$('#file').val('');
  					return;
  				}

				$.ajax({
  					url: '/verify/checkVerifyCode',
  					type: 'POST',
  					data: { 
  	  					verifyCode:$('#verifyCode').val()
  	  				},
  				})
  				.always(function(data) {
  					debugger;
  					if(data == true){
  						$.ajax({
		  					url: '/file/upload',
		  					type: 'POST',
		  					data: new FormData($('#andyForm')[0]),
		  					processData:false,
	      					contentType:false,
		  				})
		  				.always(function(data) {
		  					debugger;
		  					if(data.code == 200){
		  						BootstrapDialog.show({
			           	            title: '提示',
			           	            message: '<font color="blue" size="6px;">' + data.message + '</font>',
			           	            buttons: [{
			           	                label: '知道了',
			           	                action: function(dialog) {
			           	                    dialog.close();
			           	  //                   setTimeout(function(){
									        //     window.location = "/tools/generatefileurl"
									        // },2000)   
									        $('#fileurl').val(data.value)
									        $("#verify-code-img").src="/verify/sendcode?d=" + new Date()
			           	                	$("#verify-code-img").trigger('click')
			           	                }
			           	            }]
				           	    })
		  					} else {
		  						BootstrapDialog.show({
			           	            title: '提示',
			           	            message: '<font color="blue" size="6px;">' + data.message + '</font>',
			           	            buttons: [{
			           	                label: '知道了',
			           	                action: function(dialog) {
			           	                    dialog.close();
									        $('#fileurl').val(data.message)
									        $("#verify-code-img").src="/verify/sendcode?d=" + new Date()
			           	                	$("#verify-code-img").trigger('click')
			           	                }
			           	            }]
				           	    })
		  					}
		  				})
  					} else{
  						("#verify-code-img").src="/verify/sendcode?d=" + new Date()
			           	$("#verify-code-img").trigger('click')
  						BootstrapDialog.show({
	           	            title: '提示',
	           	            message: '<font color="blue" size="6px;">' + '验证码错误' + '</font>',
	           	            buttons: [{
	           	                label: '知道了',
	           	                action: function(dialog) {
	           	                    dialog.close();
	           	                    $('#file').val('');
	           	                }
	           	            }]
		           	    })
  					}
  				})
  				.fail(function(data) {
  					BootstrapDialog.show({
           	            title: '提示',
           	            message: '<font color="blue" size="12px;">' + '生成失败' + '</font>',
           	            buttons: [{
           	                label: '知道了',
           	                action: function(dialog) {
           	                    dialog.close();
           	                    $('#file').val('');
           	                }
           	            }]
	           	    })
  				})
  			})
  		})
  </script>
</body>
</html>
